package com.springboot.huli.controller;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot.huli.entity.Hushi;
import com.springboot.huli.entity.SysUser;
import com.springboot.huli.entity.UserRole;
import com.springboot.huli.entity.Zhuren;
import com.springboot.huli.entity.vo.HushiVo;
import com.springboot.huli.entity.vo.ZhurenVo;
import com.springboot.huli.service.IHushiService;
import com.springboot.huli.service.ISysUserService;
import com.springboot.huli.service.IUserRoleService;
import com.springboot.huli.service.IZhurenService;
import com.springboot.huli.util.RandomValidateCodeUtil;
import com.springboot.huli.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 登录控制层
 * 2020-07-08
 * author lbk
 */
@Controller
@RequestMapping("/api/login")
public class LoginController{

    private static final Logger log =  LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private IHushiService iHushiService;

    @Autowired
    private IZhurenService iZhurenService;

    @Autowired
    private IUserRoleService iUserRoleService;



    /**
     * 系统PC平台用户登陆
     * @param request
     * @param response
     * @return
     */
    @CrossOrigin
    @RequestMapping(value="/login")
    @ResponseBody
    public Result login(HttpServletRequest request, HttpServletResponse response){
        String account = request.getParameter("account");
        if(StringUtils.isEmpty(account)){
            return Result.reError("账号不能为空");
        }
        String pwd =  request.getParameter("pwd");
        if(StringUtils.isEmpty(pwd)){
            return Result.reError("密码不能为空");
        }
        String type=request.getParameter("type");
        //进行登录认证
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",account);
        queryWrapper.eq("pwd",pwd);
        queryWrapper.eq("type",type);
        SysUser hjManager=this.iSysUserService.getOne(queryWrapper);
        if(hjManager==null){
            return Result.reError("账号密码错误！！！");
        }
        StpUtil.login(hjManager.getUserid());
        return Result.reOk("登录成功");
    }

    /**
     * 注册
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/register")
    @ResponseBody
    public Result register(HttpServletRequest request, HttpServletResponse response){
        String type=request.getParameter("type");
        String account=request.getParameter("account");
        String pwd=request.getParameter("pwd");
        //验证当前账号是否存在
        QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("account",account);
        queryWrapper.eq("type",type);
        if(this.iSysUserService.getOne(queryWrapper)!=null){
            return Result.reError("账号已存在，请重新更换账号");
        }
        SysUser sysUser=new SysUser();
        sysUser.setAccount(account);
        sysUser.setPwd(pwd);
        sysUser.setCtime(new Date());
        sysUser.setType(type);
        this.iSysUserService.save(sysUser);
        if(type.equals("1")){//管理员注册
            UserRole userRole=new UserRole();
            userRole.setRoleid(1);
            userRole.setUserid(sysUser.getUserid());
            this.iUserRoleService.save(userRole);
            return Result.reOk("注册成功");
        }
        if(type.equals("2")){//主任注册
            Zhuren zhuren=new Zhuren();
            zhuren.setUserid(sysUser.getUserid());
            zhuren.setDhhm(request.getParameter("dhhm"));
            zhuren.setSex(request.getParameter("sex"));
            this.iZhurenService.save(zhuren);
            UserRole userRole=new UserRole();
            userRole.setRoleid(2);
            userRole.setUserid(sysUser.getUserid());
            this.iUserRoleService.save(userRole);
            return Result.reOk("注册成功");
        }
        if(type.equals("3")){//护士注册
            Hushi hushi=new Hushi();
            hushi.setUserid(sysUser.getUserid());
            hushi.setDhhm(request.getParameter("dhhm"));
            hushi.setSex(request.getParameter("sex"));
            this.iHushiService.save(hushi);
            UserRole userRole=new UserRole();
            userRole.setRoleid(3);
            userRole.setUserid(sysUser.getUserid());
            this.iUserRoleService.save(userRole);
            return Result.reOk("注册成功");
        }
        return Result.reError("获取注册用户信息异常");
    }


    /**
     * 获取当前登录的用户信息
     * @return
     */
    @RequestMapping("/getUserInfo")
    @ResponseBody
    public Result getUserInfo() {
        SysUser sysUser=this.iSysUserService.getById(StpUtil.getLoginIdAsString());
        if(sysUser.getType().equals("1")){
            return Result.reOk("查询成功",sysUser);
        }
        if(sysUser.getType().equals("2")){
            Zhuren zhuren=this.iZhurenService.getById(sysUser.getUserid());
            ZhurenVo zhurenVo=new ZhurenVo();
            BeanUtils.copyProperties(zhuren,zhurenVo);
            zhurenVo.setAccount(sysUser.getAccount());
            zhurenVo.setPwd(sysUser.getPwd());
            return Result.reOk("查询成功",zhurenVo);
        }
        if(sysUser.getType().equals("3")){
            Hushi hushi=this.iHushiService.getById(sysUser.getUserid());
            HushiVo hushiVo=new HushiVo();
            BeanUtils.copyProperties(hushi,hushiVo);
            hushiVo.setAccount(sysUser.getAccount());
            hushiVo.setPwd(sysUser.getPwd());
            return Result.reOk("查询成功",hushiVo);
        }
        return Result.reError("获取当前用户信息异常");
    }


    /**
     * 修改当前登录的用户信息
     * @return
     */
    @RequestMapping("/updateUserInfo")
    @ResponseBody
    public Result updateUserInfo(HushiVo hushiVo) {
        SysUser sysUser=this.iSysUserService.getById(StpUtil.getLoginIdAsString());
        if(sysUser.getType().equals("1")){
            return Result.reOk("查询成功",sysUser);
        }
        if(sysUser.getType().equals("2")){
            Zhuren zhuren=this.iZhurenService.getById(sysUser.getUserid());
            BeanUtils.copyProperties(hushiVo,zhuren,new String[]{"userid"});
            this.iZhurenService.updateById(zhuren);
            sysUser.setPwd(hushiVo.getPwd());
            this.iSysUserService.updateById(sysUser);
            return Result.reOk("修改成功");
        }
        if(sysUser.getType().equals("3")){
            Hushi hushi=this.iHushiService.getById(sysUser.getUserid());
            BeanUtils.copyProperties(hushiVo,hushi,new String[]{"userid"});
            this.iHushiService.updateById(hushi);
            sysUser.setPwd(hushiVo.getPwd());
            this.iSysUserService.updateById(sysUser);
            return Result.reOk("修改成功");
        }
        return Result.reError("获取当前用户信息异常");
    }

    /**
     * 路由地址
     */
    public String prefix = "/view/admin/account";

    /**
     * 进入管理页面
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return prefix+"/index";
    }


}
